package com.fingard.dsp.bank.directbank.ceb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheckConfirm;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.ReqBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.RetAtsBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.RetAtsBalCheckConfirmDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2019-11-11.
 * @version 1.0
 */
public class CEB01BalanceCheckConfirm  extends CEB01Base implements IBalanceCheckConfirm {

    @Override
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        //余额对账确认返回ATS的对象
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            CEB01Header bankHeader = new CEB01Header();
            bankHeader.TransCode = "b2e004011";
            bankHeader.batchID = p_req7509.reqHeader.transSeqID;
            bankHeader.JnlDateTime = new Date();
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("TransContent");
            xmlWriter.writeElementString("RequestNum", p_req7509.detailCount+"");
            xmlWriter.writeStartElement("BatchReqList");
            while (p_req7509.readRecord()){
                xmlWriter.writeStartElement("ReqData");
                xmlWriter.writeElementString("ClientPatchID", p_req7509.readedDetail.iSeqno);
                xmlWriter.writeElementString("AcNo", p_req7509.readedDetail.actNum);
                xmlWriter.writeElementString("ServiceNum", p_req7509.readedDetail.statementNumber);
                xmlWriter.writeElementString("Year", p_req7509.readedDetail.yearMonth.substring(0,4));
                xmlWriter.writeElementString("Month", p_req7509.readedDetail.yearMonth.substring(4));
                if("2".equals(p_req7509.readedDetail.dzFlag) || "7".equals(p_req7509.readedDetail.dzFlag)){
                    xmlWriter.writeElementString("IsAccord", "0");//平
                }else{
                    xmlWriter.writeElementString("IsAccord", "1");//不平
                }
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank_2(xmlWriter.toXmlString(), bankHeader.TransCode);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpReturnCode = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnCode");
                    String tmpReturnMsg = tmpBkReader.getFirstText("/Transaction/TransContent/ReturnMsg");
                    if (tmpReturnCode != null && "0000".equals(tmpReturnCode)) {
                        while(tmpBkReader.read("/Transaction/TransContent/BatchRespList/RespData")){
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpReturnMsg;
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");
                            tmpRetDetail.iSeqno = tmpBkReader.readedXElement.getSingleText("ClientPatchID");

                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        }
                    }else{
                        retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                        retAts.writeStart();//写返回ATS的统一头部信息
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpReturnMsg);
                    }

                } else {
                    retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                    retAts.writeStart();//写返回ATS的统一头部信息
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                retAts.writeStart();//写返回ATS的统一头部信息
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            this.WriteBankLogLn(e);
        }
        retAts.writeEnd();
        return retAts;
    }
}
